<?php

namespace addons\TfFutures\backend\controllers;

use addons\TfFutures\common\models\Bankinfo;
use common\models\member\Member;
use Yii;
use addons\TfFutures\common\models\Identity;
use addons\TfFutures\common\models\IdentitySearch;
use yii\web\NotFoundHttpException;

/**
 * IdentityController implements the CRUD actions for Identity model.
 */
class IdentityController extends BaseController
{
    /**
     * 身份证审核首页
     * Lists all Identity models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new IdentitySearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }


    /**
     * 编辑时的页面映射
     * @param $id
     * @return string|\yii\console\Response|\yii\web\Response
     */
    public function actionAjaxEdit($id)
    {
        if ($id == 0) {
            \yii::$app->session->setFlash('error', '请选择正确数据');
            return $this->redirect('index');
        }
        $model = Identity::findOne($id);
        $bank = Bankinfo::findOne(['member_id' => $model->member_id]);
        $model->bank = $bank;
        return $this->renderAjax('ajax-edit', ['model' => $model]);
    }

    /**
     * 映射视图
     * Displays a single Identity model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * 修改
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id, $status)
    {
        $model = $this->findModel($id);
        $user = Member::findOne($model->member_id);
        $bank = Bankinfo::find()->where(['member_id'=>(string)$model->member_id,'status'=>(string)1])->one();
        $model->status = $status;
        if ($status == 2) {
            if(!empty($bank)) {
                $bank->status = "2";
                $bank->updated_at = time();
                $bank->save();
            }
            $user->realname = $model->real_name;
            $user->save();
            $model->save();
            \yii::$app->session->setFlash('success',  '通过审核');
            return $this->redirect(['index']);
        }
        if ($status == 0) {
            if(!empty($bank)) {
                $bank->status = "0";
                $bank->updated_at = time();
                $bank->save();
            }
            $model->save();
            \yii::$app->session->setFlash('error', '驳回审核');
            return $this->redirect(['index']);
        }
        return $this->redirect(['index']);
    }

    /**
     * 删除信息
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }


    /**
     * 返回当前模型
     * @param integer $id
     * @return Identity the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Identity::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
